<?php

defined('SYSPATH') or die('Access error!');

class Controller_Admin_Post extends Controller_Admin_Main
{

    private $post;

    public function before()
    {
        parent::before();
        $this->post = Model::factory('q2a_post');
    }

    public function action_answer_list()
    {
        $id = $this->request->param('id');
        $data = array();
        $data['parent_id'] = $id;
        $data['post_type'] = Q2A_POST_TYPE_ANSWER;
        $post_list = $this->post->get_post_list($data);
        $this->template->assign('question_content', $this->post->get_var(array('id' => $id), 'content'));
        $this->template->assign('is_answer', 1);
        $this->display_list($post_list);
    }

    public function action_comment_list()
    {
        $id = $this->request->param('id');
        $data = array();
        $data['parent_id'] = $id;
        $data['post_type'] = Q2A_POST_TYPE_COMMENT;
        $post_list = $this->post->get_post_list($data);
        $this->template->assign('question_content', $this->post->get_var(array('id' => $id), 'content'));
        $this->template->assign('is_comment', 1);
        $this->display_list($post_list);
    }

    public function action_list()
    {
        $data = array();
        $data['post_type'] = Q2A_POST_TYPE_QUESTION;
        $post_list = $this->post->get_post_list($data);
        $this->display_list($post_list);
    }

    public function action_search()
    {
        $data = array();
        if ($_POST['category_id'] != '')
        {
            $data['category_id'] = $_POST['category_id'];
        }


        if ($_POST['keywords'] != '')
        {
            $data['name'] = $_POST['keywords'];
        }
        
        $data['post_type'] = Q2A_POST_TYPE_QUESTION;
        $this->display_list($this->post->get_post_list($data));
    }

    public function display_list($post_list)
    {
        $this->template->assign('category_option', $this->get_category_cache_option('q2a'));
        $this->template->assign('post_list', $post_list['list']);
        $this->template->assign('page', $post_list['page']);
        $this->template->view('post_list');
    }

    public function action_edit()
    {
        $id = $this->request->param('id');
        //获取产品的推荐位
        $recommend_id = Model::factory('recommend')->get_recommends_id($id, 'post');
        //获取推荐位类别
        $recommend_category = $this->get_category_cache_checkbox('recommend', SYSTEM_POST_RECOMMEND_CATEGORY, $recommend_id, 'recommend[]');
        if ($_POST)
        {
            $recommend_arr = isset($_POST['recommend']) ? $_POST['recommend'] : array();
            if (is_array($recommend_arr))
            {
                Model::factory('recommend')->add_recommend($id, $recommend_arr, 'post');
                Message::success(__('update success'));
                $this->request->redirect('admin/post/edit/' . $id);
            }
        }
        $post_detail = $this->post->get_post_by_id($id);
        $this->template->assign('checkboxs', $recommend_category);
        $this->template->assign('data', $post_detail);
        $this->template->view('post_edit');
    }

    public function action_delete()
    {
        $id = $this->request->param('id');
        if ($this->post->delete_post($id))
        {
            Message::success(__('delete success'));
            $this->request->redirect('admin/post/list');
        }
    }

}